import { PRISONERS_LIST } from '@/consts';
import { Modal, notification } from 'antd';
import { io } from 'socket.io-client';
import { CACHE_KEY, EVENT_TYPE, emitEvent, getCache, onEvent, setCache } from '.';

const doctorDataSource = [
  {
    room: 'A区201',
    prisoner: '嫌疑人A',
    doctor: '暂无',
    diagnosis: ``,
    medicineStatus: '未处置',
    time: '2024-05-11 14:20:00',
  },
  {
    room: 'A区201',
    prisoner: '嫌疑人B',
    prisonerSay: '鼻塞，咳嗽',
    doctor: '医生A',
    diagnosis: `病毒性感冒`,
    medicineStatus: '治疗中',
    time: '2024-05-10 15:20',
  },
  {
    room: 'A区201',
    prisoner: '嫌疑人C',
    prisonerSay: '呼吸困难',
    doctor: '医生A',
    diagnosis: `哮喘`,
    medicineStatus: '治疗中',
    time: '2024-05-10  10:20',
  },
  {
    room: 'A区201',
    prisoner: '嫌疑人D',
    prisonerSay: '牙痛',
    doctor: '医生A',
    diagnosis: `牙龈发炎`,
    medicineStatus: '已康复',
    time: '2024-03-10  10:20',
  },
];

const meetingDataSource = [
  {
    index: 0,
    name: '李律师',
    company: '君合律师事务所',
    time: `2024-01-02 20:10:00`,
    idNum: '440188198012013333',
    prisoner: '嫌疑人A',
    room: '暂无',
    position: 'A区201',
    status: '在押',
  },
  {
    index: 1,
    name: '刘律师',
    company: '未知',
    time: `2024-01-05 23:10:00`,
    idNum: '未知',
    prisoner: '嫌疑人B',
    room: '暂无',
    status: '会见完毕',
    position: 'A区201',
  },
  {
    index: 2,
    name: '尹律师',
    company: '未知',
    time: `2024-01-08 21:10:00`,
    idNum: '未知',
    prisoner: '嫌疑人C',
    room: '暂无',
    position: 'A区201',
    status: '会见完毕',
  },
  {
    index: 3,
    name: '许律师',
    company: '未知',
    time: `2024-02-08 11:10:00`,
    idNum: '未知',
    prisoner: '嫌疑人D',
    room: '暂无',
    position: 'A区201',
    status: '过期取消',
  },
];

export const initCache = () => {
  setCache(CACHE_KEY.DOCTOR_RECORD, doctorDataSource);
  setCache(CACHE_KEY.PRISION_LIST, PRISONERS_LIST);
  setCache(CACHE_KEY.MEETING_LIST, meetingDataSource);
};

export const initCacheEvent = () => {
  onEvent(EVENT_TYPE.ADD_DOCTOR_RECORD, (row) => {
    const data = getCache(CACHE_KEY.DOCTOR_RECORD);
    if (data) {
      const newData: any = [...(data || []), row];
      setCache(CACHE_KEY.DOCTOR_RECORD, newData, true);
    }
  });
};

export const initOriginalEvent = () => {
  const socket = io('wss://zkapi.lvseguanjia.cn:16064', {
    ackTimeout: 10000,
    retries: 3,
    auth: {
      serverOffset: 0,
    },
    port: 3000,
  });
  console.log('😈初始化socket');
  // currentSocket.current = socket;

  socket.on(EVENT_TYPE.RE_PC_EMERGEN_REPORT, (row) => {
    notification.warning({
      duration: null,
      message: `A区201室，` + row.prisoner + '申请报病，请处理！',
    });
    // Modal.confirm({
    //   title: '紧急报病',
    //   content: row.prisoner + '申请报病，是否立刻处理？',
    //   onOk: () => {
    //     window.location.href = '/#/doctor/record';
    //     window.location.reload();
    //   },
    //   onCancel: () => {
    //     window.location.href = '/#/doctor/record';
    //     window.location.reload();
    //   },
    // });
  });

  socket.on(EVENT_TYPE.BEGIN_ADD_TASK, (row) => {
    const data = getCache(CACHE_KEY.TASK_LIST) || [];
    console.log('[data]', data, [...data, row]);
    setCache(CACHE_KEY.TASK_LIST, [...data, row], true);
  });

  // onEvent(EVENT_TYPE.PC_EMERGEN_REPORT, (row) => {
  //   Modal.confirm({
  //     title: '紧急报病',
  //     content: row.prisoner + '申请报病，是否立刻处理？',
  //     onOk: () => {
  //       window.location.href = '/#/doctor/record';
  //       window.location.reload();
  //     },
  //     onCancel: () => {
  //       window.location.href = '/#/doctor/record';
  //       window.location.reload();
  //     },
  //   });
  // });

  onEvent(EVENT_TYPE.PC_ALARAM, (row) => {
    console.log('[row]', row);
    Modal.confirm({
      title: '紧急报警',
      content: row.prisoner + `紧急报警（级别：${row.level}）（类型：${row.type}），是否立刻处理？`,
      okText: '紧急处理',
      cancelText: '在线处理',
      onOk: () => {
        emitEvent(EVENT_TYPE.ALARAM_CONFIRM, row);
      },
      onCancel: () => {},
    });
  });
};
